import java.util.Scanner;
import java.util.Iterator;

/**
 * @author 8zhazha8
 * @create 2021-05-11-13:20
 */
public class BookMassage {

    static String[][] user =  new String[100][4];
    static String[][] book = new String[100][5];
    static String[][] pubCom = new String[100][3];
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        init();
        login();
    }
//**************************************************************************************
    private static void login() {
        while (true) {

            System.out.println("---欢迎使用图书管理系统---");
            System.out.println("请输入用户名：");
            String userName = scanner.next();
            System.out.println("请输入密码：");
            String password = scanner.next();

            boolean find = findUser(userName,password);

            if(find) {

                manageMenu();

            }
            else {

                System.out.println("该用户不存在或者密码错误！请重新登录！");
            }

        }

    }
    //**************************************************************************************
    private static void manageMenu() {
        while(true) {
            System.out.println("1 图书管理  2 出版社管理  3 退出系统");
            System.out.println("请输入数字选择功能：");
            int c = scanner.nextInt();
            if(c == 1) {
                bookManege();
            }
            else if(c == 2) {
                pubComManege();

            }
            else if(c == 3) {
                System.exit(0);
            }
            else {
                System.out.println("输入错误 重新选择！！！");
            }
        }
    }
    //**************************************************************************************
    private static void pubComManege() {
        boolean flag = true;
        while (flag) {
            System.out.println();
            System.out.println(
                    "请输入您要选择的功能：\r\n"
                            + "1.增加\r\n"
                            + "2.删除：出版社有关联的图书不能删除；\r\n"
                            + "3.更新 \r\n"
                            + "4.根据出版社名称查询\r\n"
                            + "5.查询所有出版社\r\n"
                            + "6.返回上一级菜单"
            );
            System.out.println("请输入选择的功能：");
            int num = scanner.nextInt();
            switch(num) {
                case 1:
                    addpubCom();
                    break;
                case 2:
                    deletepubCom();
                    break;
                case 3:
                    updatepubCom();
                    break;
                case 4:
                    selectpubComname();
                    break;
                case 5:
                    selectpubCom();
                    break;
                case 6:
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，请重新选择！！！");
            }
        }

    }
    //**************************************************************************************
    private static void selectpubCom() {
        System.out.println("出版社名称"+"\t"+"地址"+"\t"+"联系人");
        for (int i = 0; i < pubCom.length; i++) {
            if (pubCom[i][0] != null) {
                for (int j = 0; j < pubCom[i].length; j++) {
                    System.out.print(pubCom[i][j]+"\t");
                }
                System.out.println();
            }
        }
    }
    //**************************************************************************************
    private static void selectpubComname() {
        System.out.println("请您输入您要查询的出版社的信息：");
        String publishing=scanner.next();
        for (int i = 0; i < pubCom.length; i++) {
            if (publishing.equals(pubCom[i][0])) {
                System.out.println(pubCom[i][0]);
                System.out.println(pubCom[i][1]);
                System.out.println(pubCom[i][2]);
                break;
            }else {
                System.out.println("查询不到你要搜索的出版社");
                break;
            }
        }
    }
    //**************************************************************************************
    private static void updatepubCom() {
        System.out.println("请您输入您要修改的出版社名称：");
        String uppublishing=scanner.next();
        for (int i = 0; i < pubCom.length; i++) {
            if (uppublishing.equals(pubCom[i][0])) {
                System.out.println("输入你要修改的地址");
                String pubComaddress=scanner.next();
                System.out.println("输入你要修改的联系人");
                String pubComname=scanner.next();
                pubCom[i][0]=uppublishing;
                pubCom[i][0]=uppublishing;
                pubCom[i][0]=uppublishing;
                System.out.println("更新成功！！！");
                System.out.println("出版社名称"+"\t"+"地址"+"\t"+"联系人");
                System.out.print(pubCom[i][0]+"\t");
                System.out.print(pubCom[i][1]+"\t");
                System.out.print(pubCom[i][2]+"\t");
                break;
            }else {
                System.out.println("更新失败");
                break;
            }
        }
    }
    //**************************************************************************************
    private static void deletepubCom() {
        System.out.println("请您输入您要删除的出版社名称：");
        String publishing=scanner.next();
        for (int i = 0; i < pubCom.length; i++) {
            for (int j = 0; j < book.length; j++) {
                if (publishing.equals(book[i][0])) {
                    System.out.println("删除失败，出版社有关联的图书不能被删除！！！");
                    break;
                }else {
                    System.out.println("删除成功！！！");
                    pubCom[i][0]=null;
                    pubCom[i][1]=null;
                    pubCom[i][2]=null;
                }
            }
        }
    }
    //**************************************************************************************
    private static void addpubCom() {
        System.out.println("请输入要增加的出版社名称:");
        String pubComname = scanner.next();
        for (int i = 0; i < pubCom.length; i++) {
            if (pubComname.equals(pubCom[i][0])) {
                System.out.println("已存在该出版社信息");
                break;
            }else {
                for (int j = 0; j < pubCom.length; j++) {
                    if (pubCom[j][0] == null) {
                        System.out.println("请输入该出版社的地址：");
                        String pubComaddress=scanner.next();
                        System.out.println("请输入该出版社的联系人：");
                        String pubComcontact=scanner.next();
                        pubCom[i][0]=pubComname;
                        pubCom[i][1]=pubComaddress;
                        pubCom[i][2]=pubComcontact;
                        System.out.println("添加成功！！！");
                        System.out.println("出版社名称"+"\t"+"地址"+"\t"+"联系人");
                        System.out.print(pubCom[i][0]+"\t");
                        System.out.print(pubCom[i][1]+"\t");
                        System.out.print(pubCom[i][2]+"\t");
                        break;
                    }

                }
            }
            break;
        }
    }
    //**************************************************************************************
    private static void bookManege() {
        boolean flag = true;
        while (flag) {
            System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
            System.out.println("请输入选择的功能：");
            int c = scanner.nextInt();
            switch(c) {
                case 1:
                    addBook();
                    break;
                case 2:
                    deleteBook();
                    break;
                case 3:
                    updateBook();
                    break;
                case 4:
                    selectBook();
                    break;
                case 5:
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，请重新选择！！！");
            }
        }
    }
    //**************************************************************************************
    private static void selectBook() {
        boolean flag=true;
        while (flag) {
            System.out.println(
                    "1. 根据ISBN查询\r\n" +
                            "2. 根据书名查询（模糊）\r\n" +
                            "3. 根据出版社查询\r\n" +
                            "4. 根据作者查询\r\n" +
                            "5. 根据价格范围查询\r\n" +
                            "6. 查询所有书籍信息\r\n" +
                            "7. 返回上一级菜单"
            );
            System.out.println("请输入您要选择功能：");
            int num=scanner.nextInt();
            switch (num) {
                case 1:
                    searchByIsbn();
                    break;
                case 2:
                    searchByName();
                    break;
                case 3:
                    searchBypress();
                    break;
                case 4:
                    searchByauthor();
                    break;
                case 5:
                    searchByprice();
                    break;
                case 6:
                    searchAllBook();
                    break;
                case 7:
                    flag=false;
                    break;
                default:
                    System.out.println("输入错误 请重新输入：");
            }
        }
    }
    //**************************************************************************************
    private static void searchByprice() {
        System.out.println("请输入最低价格：");
        int min = scanner.nextInt();
        System.out.println("请输入最高价格：");
        int max = scanner.nextInt();


        for (int i = 0; i < book.length; i++) {
            if(book[i][0]!=null) {
                int price =Integer.parseInt(book[i][2]);
                if(price>=min  && price<=max) {
                    PrintBook(i);
                }
            }else {
                break;
            }
        }
    }

    //**************************************************************************************
    private static void searchByauthor() {
        System.out.println("请您输入您要查询的作者：");
        String author=scanner.next();
        for (int i = 0; i < book.length; i++) {
            if (author.equals(book[i][3])) {
                System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
                PrintBook(i);
            }
        }
    }
    //**************************************************************************************
    private static void searchBypress() {
        System.out.println("请您输入您要查询的出版社：");
        String press=scanner.next();
        for (int i = 0; i < book.length; i++) {
            if (press.equals(book[i][3]) && book[i][3] != null) {
                System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
                PrintBook(i);
            }else {
                System.out.println("输入错误！！！请重新输入");
                searchBypress();
            }
        }

    }
    //**************************************************************************************
    private static void searchByName() {
        System.out.println("请您输入您要查询的书名：");
        String BookName=scanner.next();
        System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
        for (int i = 0; i < book.length; i++) {
            if (book[i][2] != null && BookName.equals(book[i][2])  ) {
                PrintBook(i);
            }else {
                System.out.println("输入错误！！！请重新输入");
                searchByName();
            }
        }
    }
    //**************************************************************************************
    private static void searchByIsbn() {
        System.out.println("请您输入您要查询的ISBN：");
        String ISBN=scanner.next();
        int index=getIndexByISBN(ISBN);
        System.out.println("编码\t 书名 \t 价格\t 出版社 \t 作者");
        PrintBook(index);
        System.out.println();
    }
    //**************************************************************************************
    private static void PrintBook(int index) {
        for (int i = 0; i < book[index].length; i++) {
            System.out.print(book[index][i]);
        }
        System.out.println();
    }
    //**************************************************************************************
    private static void updateBook() {
        searchAllBook();
        System.out.println("请您输入您要更新的ISBN:");
        String isbn=scanner.next();
        int index=getIndexByISBN(isbn);
        if (index != -1) {
            System.out.println("请您输入您要更新的书名：");
            book[index][1]=scanner.next();
            System.out.println("请您输入您要更新的价格：");
            book[index][2]=scanner.next();
            System.out.println("请您输入您要更新的出版社：");
            book[index][3]=scanner.next();
            System.out.println("请您输入您要更新的作者：");
            book[index][4]=scanner.next();
            searchAllBook();
        }else {
            System.out.println("更新失败,没有您要更新的书籍信息！！！");
        }
    }
    //**************************************************************************************
    private static void deleteBook() {
        searchAllBook();
        System.out.println("请您输入您要删除的ISBN：");
        String isbn=scanner.next();
        int index= getIndexByISBN(isbn);
        if (index != -1) {
            for (int i = index; i < book.length-1; i++) {
                book[i]=book[i+1];
                if (book[i+1] == null) {
                    break;
                }
            }
        }
        if (book[book.length-1][0] != null) {
            for (int i = 0; i < 5; i++) {
                book[book.length-1][i] = null;
            }
            System.out.println("删除成功！！！");
            searchAllBook();
        }else {
            System.out.println("找不到您要删除的信息");
        }
    }
    //**************************************************************************************
    private static void addBook() {
        System.out.println("请输入图书ISBN:");
        String isbn = scanner.next();
        System.out.println("请输入书名:");
        String bookName = scanner.next();
        System.out.println("请输入价格:");
        String bookPrice = scanner.next();
        System.out.println("请输入出版社:");
        String pubCom = scanner.next();
        System.out.println("请输入作者:");
        String author = scanner.next();
        int index = getIndexByISBN(isbn);
        if (index == -1 ) {
            int local = getIndexFirstNUll();
            if (local != -1) {
                save(isbn,bookName,bookPrice,pubCom,author,local);
                System.out.println("新增成功");
                searchAllBook();
            }
            else {
                System.out.println("书籍数组已满，没有位置插入");
            }
        }else {
            System.out.println("isbn编码的图书已存在，不能新增");
        }
    }
    //**************************************************************************************
    private static void searchAllBook() {
        System.out.println("编码"+"\t"+"书名"+"\t"+"价格"+"\t"+"出版社"+"\t"+"作者");
        for (int i = 0; i < book.length; i++) {
            if (book[i][0] != null) {
                for (int j = 0; j < book[i].length; j++) {
                    System.out.print(book[i][j]+"\t");
                }
                System.out.println();
            }
        }
    }
    //**************************************************************************************
    private static void save(String isbn, String bookName, String bookPrice, String pubCom, String author, int local) {
        book[local][0] = isbn;
        book[local][1] = bookName;
        book[local][2] = bookPrice;
        book[local][3] = pubCom;
        book[local][4] = author;
    }
    //**************************************************************************************
    private static int getIndexFirstNUll() {
        int index = -1;
        for (int i = 0; i < book.length; i++) {
            if (book[i][0] == null) {
                index=i;
            }
            break;
        }
        return index;
    }
    //**************************************************************************************
    private static int getIndexByISBN(String isbn) {
        int index = -1;
        for (int i = 0; i < book.length; i++) {
            if (isbn.equals(book[i][0])) {
                index=i;
            }
        }
        return index;
    }
    //**************************************************************************************
    private static boolean findUser(String userName, String password) {
        boolean find = false;
        for(int i = 0; i < user.length; i++) {
            if(userName.equals(user[i][1]) && password.equals(user[i][2])) {
                find = true;
                break;
            }
        }
        return find;
    }
    //**************************************************************************************
    private static void init() {

        user[0][0] = " HP001";
        user[0][1] = "ken";
        user[0][2] ="123";
        user[0][3] ="admin";

        user[1][0] = " HP001";
        user[1][1] = "ken";
        user[1][2] ="123";
        user[1][3] ="admin";


        user[2][0] = "HP003";
        user[2][1] = "guile";
        user[2][2] = "123";
        user[2][3] = "user";

        book[0][0] = "001";
        book[0][1] = "JAVA高级编程";
        book[0][2] = "100";
        book[0][3] = "铁道部出版社";
        book[0][4] = "张三";

        book[1][0] = "002";
        book[1][1] = "SQLSERVER高级编程";
        book[1][2] = "150";
        book[1][3] = "清华出版社";
        book[1][4] = "李四";

        book[2][0] = "003";
        book[2][1] = "JAVA基础入门";
        book[2][2] = "50";
        book[2][3] = "铁道部出版社";
        book[2][4] = "张三";

        book[3][0] = "004";
        book[3][1] = "ORACLE高级编程";
        book[3][2] = "120";
        book[3][3] = "清华出版社";
        book[3][4] = "王五";

        pubCom[0][0] = "清华出版社";
        pubCom[0][1] = "武汉市XXXX";
        pubCom[0][2] = "aaa";

        pubCom[1][0] = "铁道出版社";
        pubCom[1][1] = "北京市XXXX";
        pubCom[1][2] = "bbb";

        pubCom[2][0] = "邮电出版社";
        pubCom[2][1] = "南京市XXXX";
        pubCom[2][2] = "ccc";
    }
}